#!/usr/bin/python
import os
import sys
import subprocess

# This is how it should look:
#
# Group           Test         Python 2.5 code  Python 2.6 code  Python 3.0 code
#                              2.5   2.6   3.0  2.5   2.6   3.0  2.5   2.6   3.0
# dict            slice        Yes   Yes   No   Yes   Yes   Yes   -     -     -

results = {}
test_versions = ('25', '26', '30')
python_versions = ('2.5', '2.6', '2.7', '3.1')

for pyver in python_versions:
    p = subprocess.Popen(['python'+pyver, 'runtests.py'], 
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)

    out = p.stdout.read().decode()
    lines = out.splitlines()
    for result in [str(x).split(' ') for x in lines]:
        if result[0] != 'Test':
            continue
        
        groupid, testid = result[1].split(':')
        groupid, testversion = groupid[:-2], groupid[-2:]
        if groupid not in results:
            results[groupid] = {}
        group = results[groupid]
        if testid not in group:
            group[testid] = {}
        test = group[testid]
        if testversion not in test:
            test[testversion] = {}
        version = test[testversion]
        version[pyver] = result[2] == 'succeeded'
    
# '' for the test means all tests (i.e. error on import). Expand that:
for groupid, group in results.items():
    if '' in group:
        result = group['']
        del group['']
        for testid, test in group.items():
            for testver, res in test.items():
                res.update(result.get(testver, {}))
                
# Make into lines
resultsmatrix = []
for groupid in sorted(results):
    printgroup = groupid[5:]
    for testid in sorted(results[groupid]):
        line = [printgroup, testid]
        printgroup = ''
        for testver in test_versions:
            tests = results[groupid][testid].get(testver)
            if tests is None:
                line.extend([' - ']*len(python_versions))
                continue
            for pyver in python_versions:
                if ((testver == '25' and pyver == '3.1') or
                    (testver == '30' and pyver == '2.5')):
                    # Should fail
                    line.append(tests[pyver] and '!Y!' or ' N ')
                elif ((testver == '25' and pyver != '3.1') or
                      (testver == '26' and pyver != '2.5') or
                      (testver == '30' and pyver == '3.1')):
                    # Should succeed
                    line.append(tests[pyver] and ' Y ' or '!N!')
                else:
                    # Can fail or succeed.
                    line.append(tests[pyver] and ' Y ' or ' N ')
        resultsmatrix.append(line)

# Print the lines:
print("                                        Python 2.5 code       Python 2.6 code       Python 3.0 code")
print("             Group               Test   2.5  2.6  2.7  3.1    2.5  2.6  2.7  3.1    2.5  2.6  2.7  3.1")

for line in resultsmatrix:
    print("%18s %18s   %s  %s  %s  %s    %s  %s  %s  %s    %s  %s  %s  %s" % tuple(line))
